package com.wink_172.library.activity

import android.R.attr.text
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import com.wink_172.library.R
import com.wink_172.library.databinding.ActivityShowLogBinding
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.LogUtil
import com.wink_172.library.view.ActionBarView
import org.xutils.x


/**
 *模版
 */
class ShowLogActivity : SmartActivity() {

    lateinit var binding: ActivityShowLogBinding

    companion object {

        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, ShowLogActivity::class.java).apply {
                //    putExtra(Constants.PARAM_DATA1, (String) args[0]);
                val mode = args[0] as Int
            }
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE) //无标题
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_show_log)

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN
        ) //全屏

        initView()
        init()
    }

    fun initView() {
        binding.qrCode.visibility = View.GONE
        (binding!!.actionBar as ActionBarView).updateAllContent(
            com.wink_172.library.R.mipmap.icon_back,
            "查看日志",
            "上传日志"
        );

        (binding!!.actionBar as ActionBarView).setCallback(object : ActionBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                if (event == ActionBarView.LEFT_BTN) {
                    finish()
                } else if (event == ActionBarView.RIGHT_BTN) {
                    com.wink_172.library.utils.CommonUtils.startUploadLog(object :
                        com.wink_172.library.callback.ICallback {
                        override fun onSendEvent(event: Int, vararg args: Any?) {
                            LogUtil.uploading = false
                            if (event == Constants.EventEnum.上传失败.ordinal) {
                                com.wink_172.library.utils.CommonUtils.showToast("上传失败")
                            } else if (event == Constants.EventEnum.上传成功.ordinal) {
                                val web_url = args[0] as String
                                com.wink_172.library.utils.CommonUtils.showToast("上传成功："+web_url)

                                try {
//                                    val bitmap: Bitmap =
//                                          createQRCode(web_url, 500)
                                    x.task().post {
//                                        binding.qrCode.setImageBitmap(bitmap)
                                        binding.qrCode.visibility = View.VISIBLE
                                        (binding!!.actionBar as ActionBarView).content.setText("微信扫描二维码查看日志")
                                    }

                                } catch (e: Exception) {
                                    e.printStackTrace()
                                }
                            }
                        }

                    })
                }
            }


        })
    }


    //初始化信息，网络请求
    fun init() {
        val result = LogUtil.getCurrentLog()
        binding.result.text = result
    }


}